/*
 * EvoAlghorithm.h
 *
 *  Created on: 15.11.2012
 *      Author: shadius
 */

#ifndef EVOALGHORITHM_H_
#define EVOALGHORITHM_H_
#define	 PARAMETER_COUNT 5
#define	 PI 0
#define	 CGI 1
#define	 CI 2
#define	 KI 3
#define	 DTI 4
#define PARAM_P_DEFAULT_MIN 0
#define PARAM_P_DEFAULT_MAX 1

#define PARAM_DT_DEFAULT_MIN 0
#define PARAM_DT_DEFAULT_MAX 40*60 //40 minutes in seconds
#define PARAM_CG_DEFAULT_MIN -10
#define PARAM_CG_DEFAULT_MAX 0
#define PARAM_C_DEFAULT_MIN 0
#define PARAM_C_DEFAULT_MAX 10
#include "IFastDataApprox.h"
#include <iostream>
#include <memory>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include <algorithm>
#include <vector>
#include <math.h>
#include "TimeUtils.h"
#include "INDIVIDUUM.h"
#include "INPUT_PARAMS.h"
#include "Checker.h"
using namespace std;
typedef struct _THREAD_RESULTS {
	double bestFitness;
	double bestFitnessIdx;
	double minFitness;
} THREAD_RESULTS, *LTHREAD_RESULTS;
class EvoAlghorithm {
public:
	INDIVIDUUM compute(LPAPPROXPOINT bgAPPROXPOINTS,
			LPAPPROXPOINT igAPPROXPOINTS, int len, double *average, double *max);
	EvoAlghorithm(const shared_ptr<IFastDataApprox> &BGValues,
			const shared_ptr<IFastDataApprox> &IGValues,
			const shared_ptr<vector<double>> p_ECGValues, INPUT_PARAMS params);

	virtual ~EvoAlghorithm();
private:
	shared_ptr<IFastDataApprox> p_BGValues;
	shared_ptr<IFastDataApprox> p_IGValues;
	INDIVIDUUM *population;
	shared_ptr<vector<double>> p_ECGValues;
	INPUT_PARAMS params;
	Checker checker;
	double *m_fitness;
	void initializePopulation();
	void mutate(double& mutationRate, int& iInd, LINDIVIDUUM population);
	double getIndividuumValue(INDIVIDUUM *individuum, int i);
	void updateIndividuum(INDIVIDUUM *individuum, int i, double value);
	int pickRandomIndividuum(double sumFitness);
	double random();
	double random(double min, double max);
	void findMaxAndAverageDiff(LPAPPROXPOINT bgAPPROXPOINTS,
			LPAPPROXPOINT igAPPROXPOINTS, INDIVIDUUM individuum,
			double *average, double *max, int len);
	void findKandDT(double *k, double *dt);
};

#endif /* EVOALGHORITHM_H_ */
